package de.tc.stammdatenverwaltung.gui.kontakthistory;

import de.tc.stammdatenverwaltung.functions.commonFunctions;
import de.tc.stammdatenverwaltung.gui.Main;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.JOptionPane;

public class addKontaktEintrag extends javax.swing.JDialog {

  public int ID = -1; //KontaktID
    public addKontaktEintrag(java.awt.Frame parent, boolean modal, int id) {
      super(parent, modal);
      initComponents();
      ID = id;
      jFormattedTextField1.setValue( new Date() );
    }
    @SuppressWarnings("unchecked")
  // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
  private void initComponents() {

    buttonGroup1 = new javax.swing.ButtonGroup();
    jLabel1 = new javax.swing.JLabel();
    jButton1 = new javax.swing.JButton();
    jComboBox1 = new javax.swing.JComboBox();
    jRadioButton1 = new javax.swing.JRadioButton();
    jRadioButton2 = new javax.swing.JRadioButton();
    jScrollPane1 = new javax.swing.JScrollPane();
    jTextArea1 = new javax.swing.JTextArea();
    jLabel2 = new javax.swing.JLabel();
    jLabel3 = new javax.swing.JLabel();
    jFormattedTextField1 = new javax.swing.JFormattedTextField();

    setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

    jLabel1.setText("Art");

    jButton1.setText("Einfügen");
    jButton1.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        jButton1ActionPerformed(evt);
      }
    });

    jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "E-Mail", "Fax", "Telefonat", "Brief" }));

    buttonGroup1.add(jRadioButton1);
    jRadioButton1.setText("Nachricht AN diesen Kontakt");

    buttonGroup1.add(jRadioButton2);
    jRadioButton2.setSelected(true);
    jRadioButton2.setText("Nachricht VON diesem Kontakt");

    jTextArea1.setColumns(20);
    jTextArea1.setRows(5);
    jScrollPane1.setViewportView(jTextArea1);

    jLabel2.setText("Inhalt der Nachricht");

    jLabel3.setText("Datum + Zeit");

    jFormattedTextField1.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.DateFormatter(new java.text.SimpleDateFormat("dd.MM.yyyy HH:mm:ss"))));

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
        .addContainerGap()
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
          .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 473, Short.MAX_VALUE)
          .addComponent(jRadioButton2, javax.swing.GroupLayout.Alignment.LEADING)
          .addComponent(jRadioButton1, javax.swing.GroupLayout.Alignment.LEADING)
          .addComponent(jButton1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 473, Short.MAX_VALUE)
          .addComponent(jLabel2, javax.swing.GroupLayout.Alignment.LEADING)
          .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
              .addComponent(jLabel3)
              .addComponent(jLabel1))
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
              .addComponent(jComboBox1, 0, 362, Short.MAX_VALUE)
              .addComponent(jFormattedTextField1, javax.swing.GroupLayout.DEFAULT_SIZE, 362, Short.MAX_VALUE))))
        .addContainerGap())
    );
    layout.setVerticalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(layout.createSequentialGroup()
        .addContainerGap()
        .addComponent(jButton1)
        .addGap(29, 29, 29)
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
          .addComponent(jLabel1)
          .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
          .addComponent(jLabel3)
          .addComponent(jFormattedTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
        .addComponent(jRadioButton1)
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
        .addComponent(jRadioButton2)
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
        .addComponent(jLabel2)
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 152, Short.MAX_VALUE)
        .addContainerGap())
    );

    pack();
  }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_jButton1ActionPerformed
    {//GEN-HEADEREND:event_jButton1ActionPerformed
      
      //DATUM
      String d1 = "";
      try
      {
        //einmal hin und zurueck ^^
        SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy HH:mm:ss");
        Date datum = sdf.parse( jFormattedTextField1.getText() );
        d1 = sdf.format(datum);
      }
      catch (Exception e1) { /* kein Datum */ }
      
      if (d1.length() < 1)
        d1 = "NOW";

      //EIN
      int ein = 0;
      if ( jRadioButton1.isSelected() )
        ein = 1;

      String insertQ = "insert into KONTAKTHISTORIE (IDKONTAKT, Datum, NACHRICHT, art, ein) " +
        "values ( " + ID + ", '" + d1 + "', " +
        "'" + commonFunctions.sqlSichern(jTextArea1.getText()) + "', " +
        (jComboBox1.getSelectedIndex() + 1) + ", " + ein + ")";
      try
      {
        Statement stmt = Main.con.createStatement();
        stmt.executeUpdate( insertQ );
        
        commonFunctions.load_query_to_table(Kontakthistorie.selectSQL+ "where IDKONTAKT = " + ID + " order by datum desc",
          Kontakthistorie.jTable1, Kontakthistorie.dtm );

        JOptionPane.showMessageDialog(null, "Speichern erfolgreich!",
                "Fehler!", JOptionPane.PLAIN_MESSAGE);
      }
      catch (Exception e)
      {
        JOptionPane.showMessageDialog(null, "Fehler beim einfügen - Fehler:\r\n" + e.getLocalizedMessage(),
                "Fehler!", 1);
        System.out.println( insertQ );
        e.printStackTrace();
      }
    }//GEN-LAST:event_jButton1ActionPerformed

  // Variables declaration - do not modify//GEN-BEGIN:variables
  private javax.swing.ButtonGroup buttonGroup1;
  private javax.swing.JButton jButton1;
  private javax.swing.JComboBox jComboBox1;
  private javax.swing.JFormattedTextField jFormattedTextField1;
  private javax.swing.JLabel jLabel1;
  private javax.swing.JLabel jLabel2;
  private javax.swing.JLabel jLabel3;
  private javax.swing.JRadioButton jRadioButton1;
  private javax.swing.JRadioButton jRadioButton2;
  private javax.swing.JScrollPane jScrollPane1;
  private javax.swing.JTextArea jTextArea1;
  // End of variables declaration//GEN-END:variables

}
